package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-07-25 09:34
 */
@RestController
@RequestMapping("/admin/product")
public class BaseTrademarkController {


    @Autowired
    private BaseTrademarkService baseTrademarkService;


    /**
     * 分页查询品牌列表
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result getBaseTrademarkByPage(
            @PathVariable("page") Integer page,
            @PathVariable("limit") Integer limit) {
        //查询MP提供分页方法 入参 响应类型都是Page类型 固调用业务层得到分页结果：Page
        //指定分页方法前：为分页对象Page封装当前页跟页大小
        Page<BaseTrademark> iPage = new Page<BaseTrademark>(page, limit);
        //其他属性经过分页查询后，包括：总记录数，总页数，当前页数据等
        iPage = baseTrademarkService.getBaseTrademarkByPage(iPage);
        return Result.ok(iPage);
    }


    /**
     * 保存品牌
     *
     * @param baseTrademark
     * @return
     */
    @PostMapping("/baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }


    /**
     * 根据品牌ID 查询品牌对象
     *
     * @param id
     * @return
     */
    @GetMapping("/baseTrademark/get/{id}")
    public Result getBaseTrademarkById(@PathVariable("id") Long id) {
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        return Result.ok(baseTrademark);
    }


    /**
     * 修改品牌信息
     *
     * @param baseTrademark
     * @return
     */
    @PutMapping("/baseTrademark/update")
    public Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark) {
        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result deleteBaseTrademark(@PathVariable("id") Long id){
        baseTrademarkService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据分类查询该分类下关联品牌列表
     *
     * @param category3Id
     * @return
     */
    @GetMapping("/baseCategoryTrademark/findTrademarkList/{category3Id}")
    public Result getTrademarkListByCategoryId(@PathVariable("category3Id") Long category3Id) {
        List<BaseTrademark> list = baseTrademarkService.getTrademarkListByCategoryId(category3Id);
        return Result.ok(list);
    }


    /**
     * 查询指定分类下可选品牌列表（未关联到当前品牌列表）
     *
     * @param category3Id
     * @return
     */
    @GetMapping("/baseCategoryTrademark/findCurrentTrademarkList/{category3Id}")
    public Result getCurrentTrademarkList(@PathVariable("category3Id") Long category3Id) {
        List<BaseTrademark> list = baseTrademarkService.getCurrentTrademarkList(category3Id);
        return Result.ok(list);
    }


    /**
     * 将品牌关联到指定分类
     *
     * @param vo
     * @return
     */
    @PostMapping("/baseCategoryTrademark/save")
    public Result saveCategtoryTrademark(@RequestBody CategoryTrademarkVo vo) {
        baseTrademarkService.saveCategtoryTrademark(vo);
        return Result.ok();
    }

    /**
     * 解除分类关联品牌
     *
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @DeleteMapping("/baseCategoryTrademark/remove/{category3Id}/{trademarkId}")
    public Result removeCategtoryTrademark(@PathVariable("category3Id") Long category3Id, @PathVariable("trademarkId") Long trademarkId) {
        baseTrademarkService.removeCategtoryTrademark(category3Id, trademarkId);
        return Result.ok();
    }
}
